package com.healthtop.utils;

import org.springframework.util.StringUtils;

import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

    public static final String defaultPattern = "yyyy-MM-dd";

    public static final String defaultFullPattern = "yyyy-MM-dd HH:mm:ss";

    public static String dateFormat(Date date) {
        return dateFormat(date, null);
    }

    public static String dateFormat(Date date, String pattern) {
        if(StringUtils.isEmpty(pattern)) {
            pattern = defaultPattern;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static Date parseDate(String date) throws ParseException {
        return parseDate(date, null);
    }

    public static Date parseDate(String date, String pattern) throws ParseException {
        if(!StringUtils.isEmpty(date)) {
            if(StringUtils.isEmpty(pattern)) {
                pattern = defaultPattern;
            }
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.parse(date);
        }
        return null;
    }

    public static Date getWeekStartDate(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    public static Date getWeekEndDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    public static Date getMonthFirstDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }

    public static Date getMonthEndDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    public static int getMonthDayCount(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public static Date addOrReduceDay(Date date, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, day);
        return cal.getTime();
    }

    public static Date addOrReduceWeek(Date date, int week) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.WEEK_OF_YEAR, week);
        return cal.getTime();
    }

    public static Date addOrReduceMonth(Date date, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, month);
        return cal.getTime();
    }

    public static int getYear() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.YEAR);
    }

    public static int getMonth() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.MONTH) + 1;
    }

    public static List<Date> dateList(Date start, Date end) {
        List<Date> list = new ArrayList<>(0);
        Date date = start;
        while(compareDate(date, end) != 1) {
            list.add(date);
            //System.out.println(dateFormat(date));
            date = addOrReduceDay(date, 1);
        }
        return list;
    }

    public static List<Date> dateList(Date start, Date end, int weekday){
        List<Date> list = new ArrayList<>(0);
        Date date = start;
        while(compareDate(date, end) != 1) {
            int w = getDateOfWeek(date);
            if(weekday==w) {
                list.add(date);
                //System.out.println(dateFormat(date));
            }
            date = addOrReduceDay(date, 1);
        }
        return list;
    }

    public static int compareDate(Date d1, Date d2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        c1.setTime(d1);
        c2.setTime(d2);

        int result = c1.compareTo(c2);
        return result;
    }

    public static boolean compareDate(Date d1) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();

        c1.setTime(d1);
        c1.add(Calendar.MINUTE, 30);
        c2.setTime(new Date());

        int result = c1.compareTo(c2);
        if (result >= 0)
            return true;
        else
            return false;
    }

    /**
     * 星期的第一天是礼拜天
     * @param dt
     * @return
     */
    public static int getDateOfWeek(Date dt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK);
        return w;
    }

    public static int timeToInt(String time) {
        String[] times = time.split(":");
        return Integer.parseInt(times[0]) * 60 + Integer.parseInt(times[1]);
    }

    /**
     * 星期的第一天是礼拜天
     * @param firstDate,请传入一个月的第一天
     * @return
     */
    public static int[] getWeekdaysInMonth(Date firstDate) {
        int monthDayCount = DateUtil.getMonthDayCount(firstDate);
        int w = getDateOfWeek(firstDate);
        int[] weekdays = new int[monthDayCount];
        for(int i=0;i<weekdays.length;i++) {
            weekdays[i] = w;
            //System.out.println(weekdays[i]);
            if(w == 7) {
                w = 1;
            } else {
                w++;
            }
        }
        return weekdays;
    }

    public static void main(String[] args) throws Exception {
        //dateList(parseDate("2018-07-04"), parseDate("2018-08-06"), 4);
//        System.out.println(getMonth());
//        dateList(parseDate("2018-09-03"), parseDate("2018-09-09"));
        Date nowEndTime = DateUtil.parseDate("2018-09-06 17:04:00", DateUtil.defaultFullPattern);
        System.out.println(compareDate(nowEndTime));
    }

}
